﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;

namespace LynxWebLog
{
    // Note: For instructions on enabling IIS7 classic mode, 
    // visit http://go.microsoft.com/fwlink/?LinkId=301868
    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            Application["webApiRoot"] = ConfigurationManager.AppSettings["webApiRoot"];

            AreaRegistration.RegisterAllAreas();

            //IdentityConfig.ConfigureIdentity();
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);


            LynxWebLog.BlogRoot.SiteUrl = ConfigurationManager.AppSettings["SiteUrl"];
            LynxWebLog.BlogRoot.MaxAttachmentSize = ConfigurationManager.AppSettings["MaxAttachmentSize"];

            BlogRoot.MwlAPI = new MetaWebLogAPI();

            Dictionary<System.Net.IPAddress, DateTime> blogWriteGovernor = new Dictionary<System.Net.IPAddress, DateTime>();

            List<System.Net.IPAddress> blogGovernorOverrideIP = new List<System.Net.IPAddress>();
            if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["BlogGovernorOverrideIP"])) {
                string[] ipList = (ConfigurationManager.AppSettings["BlogGovernorOverrideIP"]).Split(',');
                foreach (string item in ipList) {
                    blogGovernorOverrideIP.Add(System.Net.IPAddress.Parse(item.Trim()));
                }
            }
            List<IPAddressRange> blogGovernorOverrideIPRange = new List<IPAddressRange>();
            if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["BlogGovernorOverrideIPRange"])) {
                string[] ipRangeList = ConfigurationManager.AppSettings["BlogGovernorOverrideIPRange"].Split(';');
                foreach (string item in ipRangeList) {
                    string[] ipRange = item.Split('-');
                    blogGovernorOverrideIPRange.Add(new IPAddressRange(IPAddress.Parse(ipRange[0].Trim()), IPAddress.Parse(ipRange[1].Trim())));
                }
            }
            List<System.Net.IPAddress> blogBlacklistIP = new List<System.Net.IPAddress>();
            if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["BlogBlacklistIP"])) {
                string[] ipList = (ConfigurationManager.AppSettings["BlogBlacklistIP"]).Split(',');
                foreach (string item in ipList) {
                    blogBlacklistIP.Add(System.Net.IPAddress.Parse(item.Trim()));
                }
            }
            List<IPAddressRange> blogBlacklistIPRange = new List<IPAddressRange>();
            if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["BlogBlacklistIPRange"])) {
                string[] ipRangeList = ConfigurationManager.AppSettings["BlogBlacklistIPRange"].Split(';');
                foreach (string item in ipRangeList) {
                    string[] ipRange = item.Split('-');
                    blogBlacklistIPRange.Add(new IPAddressRange(IPAddress.Parse(ipRange[0].Trim()), IPAddress.Parse(ipRange[1].Trim())));
                }
            }
            int governorWriteFreq = 60;
            if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["GovernorWriteFreq"])) {
                int.TryParse(ConfigurationManager.AppSettings["GovernorWriteFreq"], out governorWriteFreq);
            }
            HttpContext.Current.Application.Add("__BlogWriteGovernor", blogWriteGovernor);
            HttpContext.Current.Application.Add("__BlogGovernorOverrideIP", blogGovernorOverrideIP);
            HttpContext.Current.Application.Add("__BlogGovernorOverrideIPRange", blogGovernorOverrideIPRange);
            HttpContext.Current.Application.Add("__BlogBlacklistIP", blogBlacklistIP);
            HttpContext.Current.Application.Add("__BlogBlacklistIPRange", blogBlacklistIPRange);
            HttpContext.Current.Application.Add("__GovernorWriteFreq", governorWriteFreq);

            List<string> validLicenses = new List<string>();
            if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["ValidLicense"])) {
                validLicenses = ConfigurationManager.AppSettings["ValidLicense"].Split(':').ToList();
            }
            HttpContext.Current.Application.Add("__ValidLicenseList", validLicenses);
            HttpContext.Current.Application.Add("__CreativeCommons", "off");
            HttpContext.Current.Application.Add("__DefaultLicense", "NONE");
            if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["CreativeCommons"])) {
                HttpContext.Current.Application.Remove("__CreativeCommons");
                HttpContext.Current.Application.Add("__CreativeCommons", ConfigurationManager.AppSettings["CreativeCommons"]);
            }
            if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["DefaultLicense"]) && (string)HttpContext.Current.Application["__CreativeCommons"]  == "on") {
                HttpContext.Current.Application.Remove("__DefaultLicense");
                HttpContext.Current.Application.Add("__DefaultLicense", ConfigurationManager.AppSettings["DefaultLicense"]);
            }
            bool _commentsAllowed = false;
            if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["CommentsAllowed"])) {
                _commentsAllowed = Convert.ToBoolean(ConfigurationManager.AppSettings["CommentsAllowed"]);
            }
            HttpContext.Current.Application.Add("__CommentsAllowed", _commentsAllowed);
        }
    }
}
